// pages/class/editClass/editClass.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cname:'',//班级名称
    classNumber:'',//班级人数
    cyear:'',//所在年级
    cttid :'',//管理员姓名
    ctel:'',//电话号码
    detail: [], //详情图片
    detailNew: [],
    detailAll: [],
    checkUp: true, //判断从编辑页面进来是否需要上传图片
    check: false,
    chooseViewShowDetail: true,
    params:{},
    dis:false
  },
  //获取班级名称输入框的值
  className(e){
    this.setData({
      cname:e.detail.value
    })
  },
  // 获取班级人数
  classNumber(e){
    this.setData({
      classNumber:e.detail.value
    })
  },
  //获取所在年级
  cyear(e){
    this.setData({
      cyear:e.detail.value
    })
  },
  //获取管理员姓名
  administratorsName(e){
    this.setData({
      cttid:e.detail.value
    })
  },
  // 获取电话号码
  phoneNumber(e){
    this.setData({
      ctel:e.detail.value
    })
  },

  /**判断详情新旧数组是否有相同值 */
  checkDetail() {
    let detail = this.data.detail
    let detailNew = this.data.detailNew
    let detailAll = this.data.detailAll
    for (var i = 0; i < detail.length; i++) {
      for (var j = 0; j < detailNew.length; j++) {
        if (detail[i] === detailNew[j]) {
          detailAll = detailAll.concat(detail[i])
          this.setData({
            detailAll: detailAll
          })
        } else {
          console.log("detail无相同")
        }
      }
    }
  },
  //添加上传图片
  chooseImageTap: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseDetail('album')
          } else if (res.tapIndex == 1) {
            that.chooseDetail('camera')
          }
        }
      }
    })
  },
  /** 选择图片detail */
  chooseDetail: function (type) {
    wx.showLoading({
      title: '正在打开',
      mask: 'true'
    })
    var that = this;
    if (that.data.detail.length < 1) {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: [type],
        success: function (res) {
          console.log(type)
          wx.hideLoading();
          // detail中包含的可能还有编辑页面下回显的图片，detailNew中包含的只有所选择的图片
          let detail = that.data.detail;
          detail = detail.concat(res.tempFilePaths);
          let detailNew = that.data.detailNew
          detailNew = detailNew.concat(res.tempFilePaths)
          that.setData({
            detail: detail,
            detailNew: detailNew,
            checkUp: false
          })
          that.chooseViewShowDetail();
        },
        complete: function () {
          wx.hideLoading()
        }
      })
    } else {
      wx.showToast({
        title: '限制选择1个文件',
        icon: 'none',
        duration: 1000
      })
    }
  },
  /** 删除图片detail */
  deleteImvDetail: function (e) {
    var that = this;
    var detail = that.data.detail;
    var itemIndex = e.currentTarget.dataset.id;
    if (that.data.productID != 0) {
      wx.showModal({
        title: '提示',
        content: '删除不可恢复，请谨慎操作',
        success(res) {
          if (res.confirm) {
            detail.splice(itemIndex, 1);
            that.setData({
              detail: detail,
              checkUp: false
            })
            that.chooseViewShowDetail();
          }
        }
      })
    } else {
      detail.splice(itemIndex, 1);
      that.setData({
        detail: detail,
        checkUp: false
      })
      that.chooseViewShowDetail();
    }
  },
  /** 是否隐藏图片选择detail */
  chooseViewShowDetail: function () {
    if (this.data.detail.length >= 1) {
      this.setData({
        chooseViewShowDetail: false
      })
    } else {
      this.setData({
        chooseViewShowDetail: true
      })
    }
  },
  /** 查看大图Detail */
  showImageDetail: function (e) {
    var detail = this.data.detail;
    var itemIndex = e.currentTarget.dataset.id;
    wx.previewImage({
      current: detail[itemIndex], // 当前显示图片的http链接
      urls: detail // 需要预览的图片http链接列表
    })
  },

  //发布修改
  formSubmit(e){
    console.log(e)
    let that = this
    var telTF = /^(?<national>\+?(?:86)?)(?<separator>\s?-?)(?<phone>(?<vender>(13|15|18)[0-9])(?<area>\d{4})(?<id>\d{4}))$/
    if(e.detail.value.cname===""){
      wx.showToast({
        title: '请输入班级名称',
        icon:"none",
        duration:1000,
        mask:true
      })
    }else if(e.detail.value.cname.length > 20){
      wx.showToast({
        title: '班级名称不得大于20字',
        icon:'none',
        duration:1000,
        mask:true
      })
    }else if(e.detail.value.cyear===""){
      wx.showToast({
        title: '请选择所在年级',
        icon:'none',
        duration:1000,
        mask:true
      })
    }else if(e.detail.value.cttid===""){
      wx.showToast({
        title: '请输入管理员姓名',
        icon:'none',
        duration:1000,
        mask:true
      })
    }else if(e.detail.value.ctel===""){
      wx.showToast({
        title: '请输入电话号码',
        icon:'none',
        duration:1000,
        mask:true
      })
    }else if(!telTF.test(e.detail.value.ctel)){
      wx.showToast({
        title: '请输入正确的电话号码',
        icon:'none',
        duration:1000,
        mask:true
      })
    }else if(that.data.detail.length===0){
      wx.showToast({
        title: '请上传班级头像',
        icon:"none",
        duration:1000,
        mask:true
      })
    }else{
      let params = {
        cid:Date.parse(new Date()),
        cname:e.detail.value.cname,
        cyear:e.detail.value.cyear,
        cttid:e.detail.value.cttid,
        ctel:e.detail.value.ctel
      }
      console.log(params)
      wx.showModal({
        title: '提示',
        content: '确定创建班级',
        success(res) {
          if (res.confirm) {
            that.sureRelease(params); //确定
          } else if (res.cancel) {
            wx.showToast({
              title: '继续编辑',
              icon: "none",
              duration: 2000,
              mask: true,
            })
          }
          that.setData({
            dis: true,
          })
        }
      })
    }
  },
/**确认发布 */
sureRelease(params) {
  console.log(params)
  let id = params.cid;
  let cid = id.toString().slice(0,9)
  let that = this
  for (var j = 0; j < that.data.detail.length; j++) {
    if (that.data.detail.length === j + 1) {
      that.data.check = true
    }
    // console.log(that.data.detail[j])
    wx.uploadFile({
      url: 'http://101.43.154.9:8001/upload',
      filePath: that.data.detail[j],
      name: 'file',
      success: function (res) {
        console.log(res)
        let imgdata = res.data;
        console.log(imgdata)
        wx.request({
          url: 'http://101.43.154.9:8001/classadd',
          method:'POST',
          data:{
            cid:cid,
            cimg:`http://101.43.154.9:8001/image/${imgdata}`,
            cyear: params.cyear,
            cttid: params.cttid,
            ctel:params.ctel,
            cname:params.cname
          },
          success:(res)=>{
            console.log(res)
            wx.showToast({
              title: '班级创建成功',
              icon: "none",
              duration: 2000,
              mask: true,
              success() {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 0,
                  })
                }, 1000);
              }
            })
          }
        })
      },
      fail: function (res) {
        // if (JSON.parse(res.errMsg) === "request:fail socket time out timeout:6000") {
        wx.showToast({
          title: '请求超时，请稍后再试！',
          icon: "none",
          duration: 2000,
          mask: true,
          success() {
            setTimeout(function () {
              wx.navigateBack({
                delta: 0,
              })
            }, 1000);
          }
        })
        // }

      }
    })
  }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})